package com.kk.crowd.test;


import com.kk.crowd.service.api.AdminService;
import com.kk.crowd.entity.Admin;
import com.kk.crowd.entity.Role;
import com.kk.crowd.mapper.AdminMapper;
import com.kk.crowd.mapper.RoleMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

// 指定 Spring 给 Junit 提供的运行器类
@RunWith(SpringJUnit4ClassRunner.class)
// 加载 Spring 配置文件的注解
@ContextConfiguration(locations = {"classpath:spring-persist-mybatis.xml", "classpath:spring-persist-tx.xml"})
public class SpringTestOne {
    @Autowired
    private DataSource dataSource;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleMapper roleMapper;

    @Test
    public void testTx() {
        Admin admin = new Admin (null, "tom", "123123", "汤姆", "tom@qq.com", null);
        adminService.saveAdmin (admin);
    }

    @Test
    public void testDataSource() throws SQLException {
        // 1.通过数据源对象获取数据源连接
        Connection connection = dataSource.getConnection ( );

        //
        System.out.println (connection);
    }

    @Test
    public void testLogf() {
        //获取日志记录对象
        Logger logger = LoggerFactory.getLogger (SpringTestOne.class);

        //按照日志级别打印
        logger.debug ("debut **************************************");
        logger.info ("info ***************************************");
        logger.error ("error **************************************");

    }


    @Test
    public void testadd() {
        Admin admin = new Admin (null, "jerry", "123456", "杰瑞", "jerry@qq.com", null);
        int insert = adminMapper.insert (admin);
        System.out.println ("记录数：" + insert);
    }

    @Test
    public void testsaveAdmin() {
        for (int i = 0; i < 352; i++) {
            adminMapper.insert (new Admin (null, "loginAcct" + i, "userPswd" + i, "userName" + i, "email" + i + "@qq.com", null));
        }
    }

    @Test
    public void testsaveRole() {
        for (int i = 0; i < 352; i++) {
            roleMapper.insert (new Role ("role" + i));
        }
    }

}
